//package com.wz.erp.upload;
//
//import org.apache.commons.io.FilenameUtils;
//import org.apache.commons.lang.StringUtils;
//import org.apache.commons.lang.time.DateFormatUtils;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartHttpServletRequest;
//import org.springframework.web.multipart.commons.CommonsMultipartResolver;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpSession;
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.util.Iterator;
//
///**
// * Created by lxy on 2017/11/14.
// */
//@Controller
//@RequestMapping("/upload")
//public class UploadController {
//
//    @RequestMapping(value = "/upload/file",method = { RequestMethod.GET})
//    public void uploadFile(HttpServletRequest request)throws Exception{
//        long  startTime=System.currentTimeMillis();
//        //灏嗗綋鍓嶄笂涓嬫枃鍒濆鍖栫粰  CommonsMutipartResolver 锛堝閮ㄥ垎瑙ｆ瀽鍣級
//        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
//                request.getSession().getServletContext());
//        //妫�鏌orm涓槸鍚︽湁enctype="multipart/form-data"
//        if(multipartResolver.isMultipart(request))
//        {
//            //灏唕equest鍙樻垚澶氶儴鍒唕equest
//            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
//            //鑾峰彇multiRequest 涓墍鏈夌殑鏂囦欢鍚�
//            Iterator iter=multiRequest.getFileNames();
//
//            while(iter.hasNext())
//            {
//                //涓�娆￠亶鍘嗘墍鏈夋枃浠�
//                MultipartFile file=multiRequest.getFile(iter.next().toString());
//                if(file!=null)
//                {
//                    String path="E:/springUpload"+file.getOriginalFilename();
//                    //涓婁紶
//                    file.transferTo(new File(path));
//                }
//
//            }
//
//        }
//        long  endTime=System.currentTimeMillis();
//        System.out.println("鏂规硶涓夌殑杩愯鏃堕棿:"+String.valueOf(endTime-startTime)+"ms");
//    }
//
//    public static String uploadDiy(MultipartHttpServletRequest request,String img) {
//        HttpSession session=request.getSession();
//        String fileName = "";
//        String suffix = "";
//        String saveFileName = "";
//        String savessName = "";
////		String nowTimeStr = "";
////		SimpleDateFormat sDateFormat;
////		Random r = new Random();
//        FileOutputStream fileOutputStream = null;
//        MultipartFile f = request.getFile(img);
//        //鏂囦欢淇濆瓨浣嶇疆锛屽綋鍓嶉」鐩笅鐨剈pload/attachment
//        String uploadDir="upload" + File.separatorChar + "attachment" +File.separatorChar+"user"+ File.separatorChar;
//        //姣忓ぉ涓婁紶鐨勬枃浠舵牴鎹棩鏈熷瓨鏀惧湪涓嶅悓鐨勬枃浠跺す
//        String autoCreatedDateDirByParttern = "yyyy" + File.separatorChar + "MM" + File.separatorChar + "dd"
//                + File.separatorChar;
//
//        String autoCreatedDateDir = DateFormatUtils.format(new java.util.Date(), autoCreatedDateDirByParttern);
//        String ctxDir = session.getServletContext().getRealPath(String.valueOf(File.separatorChar));
//        if (!ctxDir.endsWith(String.valueOf(File.separatorChar))) {
//            ctxDir = ctxDir + File.separatorChar;
//        }
//        String path =ctxDir + uploadDir + autoCreatedDateDir;
//        File filePath = new File(path);
//        if (!filePath.exists()){
//            filePath.mkdirs();
//        }
//
//        if (!f.isEmpty()) {
//            fileName = f.getOriginalFilename();
//            suffix = StringUtils.join(new String[] { java.util.UUID.randomUUID().toString(), ".", FilenameUtils.getExtension(fileName) });
//
//            saveFileName =path+suffix;
//
//            File files = new File(saveFileName);
//            try {
//                fileOutputStream = new FileOutputStream(files);
//                fileOutputStream.write(f.getBytes());
//                fileOutputStream.flush();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            savessName = uploadDir + autoCreatedDateDir+suffix;
//            if (fileOutputStream != null) {
//                try {
//                    fileOutputStream.close();
//                } catch (IOException ie) {
//                    ie.printStackTrace();
//                }
//            }
//        }
//        return savessName;
//    }
//
//}
